﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QUAN_LY_MUON_TRA_SACH
{
    public partial class frmdangnhap : Form
    {
        public frmdangnhap()
        {
            InitializeComponent();
        }

        private void frmdangnhap_Load(object sender, EventArgs e)
        {
            txtname.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Connection.Close();
            Application.Exit();
        }
        SqlCommand lenh;
        SqlDataReader sql;
        private void btndangnhap_Click(object sender, EventArgs e)
        {
            ConSQL kn = new ConSQL();
            kn.ServerName = "HAILIEN-VAIO\\SQLEXPRESS";
            kn.DatabaseName = "QL_MUON_TRA_SACH_THU_VIEN";
            Connection.ConnectionString = kn.GetConnectionString();
            
            Connection.Connect();
           
            if(txtname.Text=="" || txtpass.Text=="")
                MessageBox.Show("Bạn chưa nhập username hoặc password!", "Đăng nhập thất bại", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else if (rdbnhanvien.Checked)
            {
                bool found = false;
                
                try
                {
                    
                    lenh = new SqlCommand("select * from nhan_vien;",Connection.sqlConnection);
                    sql = lenh.ExecuteReader();

                    while (sql.Read() && found == false)
                    {
                        if (txtname.Text == sql.GetString(0) && txtpass.Text == sql.GetString(6)&&sql.GetString(5)=="nhân viên")
                        {
                            found = true;
                            MessageBox.Show("ádsad");
                            FORM_MAIN.frmmain.Show();
                            MessageBox.Show("hgjgd");
                            FORM_MAIN.frmmain.siInfo.Caption = sql.GetString(1);
                            FORM_MAIN.frmmain.barchucvu.Caption = sql.GetString(5);
                            FORM_MAIN.frmmain.barmsnv.Caption = sql.GetString(0);
                            FORM_MAIN.frmmain.barlogtime.Caption = "Thời Gian Đăng Nhập:";
                            FORM_MAIN.frmmain.bartime.Caption = DateTime.Now.ToString();
                            FORM_MAIN.frmmain.siInfo.Enabled = true;
                            FORM_MAIN.frmmain.barchucvu.Enabled = true;
                            FORM_MAIN.frmmain.bartime.Enabled = true;
                            FORM_MAIN.frmmain.barmsnv.Enabled = true;
                        }
                        
                    }
                    sql.Close();
                    
                    if (!found) MessageBox.Show("Bạn đã nhập username hoặc password không đúng!", "Đăng nhập thất bại", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else
                    {
                        this.Hide();
                        
                        FORM_MAIN.frmmain.barbtndangxuat.Enabled = true;
                        FORM_MAIN.frmmain.barbtndoipass.Enabled = true;
                        
                        FORM_MAIN.frmmain.barbtndsdocgia.Enabled = true;
                        FORM_MAIN.frmmain.barthemdocgia.Enabled = true;
                        FORM_MAIN.frmmain.barsuadocgia.Enabled = true;
                        FORM_MAIN.frmmain.barbtnhuydocgia.Enabled = true;
                        FORM_MAIN.frmmain.barbtnloaidg.Enabled = true;
                        FORM_MAIN.frmmain.barbtntacgia.Enabled = true;
                        
                        FORM_MAIN.frmmain.barbtnmuon.Enabled = true;
                        FORM_MAIN.frmmain.barsachdangmuon.Enabled = true;
                        FORM_MAIN.frmmain.barbtntra.Enabled = true;
                        FORM_MAIN.frmmain.barbtnphat.Enabled = true;
                        FORM_MAIN.frmmain.barbtndsphat.Enabled = true;
                        
                        FORM_MAIN.frmmain.barbtntim.Enabled = true;
                        FORM_MAIN.frmmain.bardnhap.Enabled = true;
                        FORM_MAIN.frmmain.bardxuat.Enabled = true;
                        FORM_MAIN.frmmain.barmuon.Enabled = true;
                        FORM_MAIN.frmmain.bartra.Enabled = true;
                        FORM_MAIN.frmmain.barsaoluu.Enabled = true;
                        FORM_MAIN.frmmain.barphuchoi.Enabled = true;
                        FORM_MAIN.frmmain.barthemdg.Enabled = true;
                        FORM_MAIN.frmmain.barhuydg.Enabled = true;
                        FORM_MAIN.frmmain.barphat.Enabled = true;
                        FORM_MAIN.frmmain.bartracuc.Enabled = true;
                        //
                        FORM_MAIN.frmmain.barbtndangnhap.Enabled = false;
                        FORM_MAIN.frmmain.bardnhap.Enabled = false;
                        //
                        FORM_MAIN.frmmain.xtraTabControl1.Enabled = true;
                        //
                        lenh = new SqlCommand("select PATH_ANH from dau_sach", Connection.sqlConnection);
                        sql = lenh.ExecuteReader();
                        PictureBox[] manghinh = { FORM_MAIN.frmmain.pictureBox1, FORM_MAIN.frmmain.pictureBox2, FORM_MAIN.frmmain.pictureBox3, FORM_MAIN.frmmain.pictureBox4, FORM_MAIN.frmmain.pictureBox5, FORM_MAIN.frmmain.pictureBox6, FORM_MAIN.frmmain.pictureBox7, FORM_MAIN.frmmain.pictureBox8, FORM_MAIN.frmmain.pictureBox9, FORM_MAIN.frmmain.pictureBox10, FORM_MAIN.frmmain.pictureBox11, FORM_MAIN.frmmain.pictureBox12 };
                        int i = 0;
                        while (sql.Read() && i < 12)
                        {
                            Image hinh = Image.FromFile(sql.GetString(0));
                            manghinh[i].Image = hinh;
                            //manghinh[i].Show();
                            i++;
                        }
                        sql.Close();
                    }
                    
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Đăng Nhập Thất Bại "+ex.Message);
                }
                
            }
            else if (rdbquanly.Checked)
            {
                bool found = false;
                try
                {
                    lenh = new SqlCommand("select * from nhan_vien;", Connection.sqlConnection);
                    sql = lenh.ExecuteReader();
                    while (sql.Read() && found == false)
                    {
                        if (txtname.Text == sql.GetString(0) && txtpass.Text == sql.GetString(6) && sql.GetString(5) == "quản lý")
                        {
                            found = true;
                            FORM_MAIN.frmmain.Show();
                            FORM_MAIN.frmmain.siInfo.Caption = sql.GetString(1);
                            FORM_MAIN.frmmain.barchucvu.Caption = sql.GetString(5);
                            FORM_MAIN.frmmain.barmsnv.Caption = sql.GetString(0);
                            FORM_MAIN.frmmain.barlogtime.Caption = "Thời Gian Đăng Nhập:";
                            FORM_MAIN.frmmain.bartime.Caption = DateTime.Now.ToString();
                            FORM_MAIN.frmmain.siInfo.Enabled = true;
                            FORM_MAIN.frmmain.barchucvu.Enabled = true;
                            FORM_MAIN.frmmain.bartime.Enabled = true;
                            FORM_MAIN.frmmain.barmsnv.Enabled = true;
                        }
                    }
                    sql.Close();
                    if (!found) MessageBox.Show("Bạn đã nhập username hoặc password không đúng!", "Đăng nhập thất bại", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    else
                    {
                        this.Hide();

                        FORM_MAIN.frmmain.barbtndangxuat.Enabled = true;
                        FORM_MAIN.frmmain.barbtndoipass.Enabled = true;
                        FORM_MAIN.frmmain.barbtnsaoluu.Enabled = true;
                        FORM_MAIN.frmmain.barbtnphuchoi.Enabled = true;
                        FORM_MAIN.frmmain.barbtndsdocgia.Enabled = true;
                        FORM_MAIN.frmmain.barthemdocgia.Enabled = true;
                        FORM_MAIN.frmmain.barsuadocgia.Enabled = true;
                        FORM_MAIN.frmmain.barbtnhuydocgia.Enabled = true;
                        FORM_MAIN.frmmain.barbtnloaidg.Enabled = true;
                        FORM_MAIN.frmmain.barbtntacgia.Enabled = true;

                        FORM_MAIN.frmmain.barbtnmuon.Enabled = true;
                        FORM_MAIN.frmmain.barsachdangmuon.Enabled = true;
                        FORM_MAIN.frmmain.barbtntra.Enabled = true;
                        FORM_MAIN.frmmain.barbtnphat.Enabled = true;
                        FORM_MAIN.frmmain.barbtndsphat.Enabled = true;

                        FORM_MAIN.frmmain.barbtntim.Enabled = true;
                        FORM_MAIN.frmmain.bardnhap.Enabled = true;
                        FORM_MAIN.frmmain.bardxuat.Enabled = true;
                        FORM_MAIN.frmmain.barmuon.Enabled = true;
                        FORM_MAIN.frmmain.bartra.Enabled = true;
                        FORM_MAIN.frmmain.barsaoluu.Enabled = true;
                        FORM_MAIN.frmmain.barphuchoi.Enabled = true;
                        FORM_MAIN.frmmain.barthemdg.Enabled = true;
                        FORM_MAIN.frmmain.barhuydg.Enabled = true;
                        FORM_MAIN.frmmain.barphat.Enabled = true;
                        FORM_MAIN.frmmain.bartracuc.Enabled = true;
                        FORM_MAIN.frmmain.barsachmuonnhieu.Enabled = true;
                        FORM_MAIN.frmmain.barsachkomuon.Enabled = true;
                        FORM_MAIN.frmmain.bardsquahan.Enabled = true;
                        FORM_MAIN.frmmain.bardsbiphat.Enabled = true;
                        FORM_MAIN.frmmain.barmuontheotloai.Enabled = true;
                        FORM_MAIN.frmmain.barmuontungnam.Enabled = true;
                        FORM_MAIN.frmmain.bardsmuontheoquyen.Enabled = true;
                        FORM_MAIN.frmmain.barmuonnhieu.Enabled = true;
                        FORM_MAIN.frmmain.barkomuon.Enabled = true;
                        FORM_MAIN.frmmain.barquahan.Enabled = true;
                        FORM_MAIN.frmmain.bardsphat.Enabled = true;
                        FORM_MAIN.frmmain.barlmnam.Enabled = true;
                        FORM_MAIN.frmmain.barlmquyen.Enabled = true;
                        FORM_MAIN.frmmain.barlmtl.Enabled = true;
                        FORM_MAIN.frmmain.barbaocao.Enabled = true;
                        //
                        FORM_MAIN.frmmain.barbtndangnhap.Enabled = false;
                        FORM_MAIN.frmmain.bardnhap.Enabled = false;

                        FORM_MAIN.frmmain.xtraTabControl1.Enabled = true;
                        //
                        lenh = new SqlCommand("select PATH_ANH from dau_sach", Connection.sqlConnection);
                        sql = lenh.ExecuteReader();
                        PictureBox[] manghinh = { FORM_MAIN.frmmain.pictureBox1, FORM_MAIN.frmmain.pictureBox2, FORM_MAIN.frmmain.pictureBox3, FORM_MAIN.frmmain.pictureBox4, FORM_MAIN.frmmain.pictureBox5, FORM_MAIN.frmmain.pictureBox6, FORM_MAIN.frmmain.pictureBox7, FORM_MAIN.frmmain.pictureBox8, FORM_MAIN.frmmain.pictureBox9, FORM_MAIN.frmmain.pictureBox10, FORM_MAIN.frmmain.pictureBox11, FORM_MAIN.frmmain.pictureBox12 };
                        int i = 0;
                        while (sql.Read() && i < 12)
                        {
                            Image hinh = Image.FromFile(sql.GetString(0));
                            manghinh[i].Image = hinh;
                            //manghinh[i].Show();
                            i++;
                        }
                        sql.Close();
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Đăng Nhập Thất Bại " + ex.Message);
                }
                finally
                {
                    sql.Close();
                }
               
            }
          
        }
        
    }
    
}
